package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.app.service.IAppMineService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      更新用户的家属开门密码队列消费者。
 * </desc>
 *
 * @createDate 2017/9/6
 */
@Component
public class UpdateFamilyOpenPwdConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateFamilyOpenPwdConsumer.class);

    @Autowired
    IAppMineService iAppMineService;
    /**
     * <desc>
     *      RabbitMQ_接收app用户id及项目id及开门密码，更新用户的家属开门密码
     * <desc/>
     *
     * @param   message 消息数据
     * @author Jiaqi.X
     * @createDate 2017/12/07
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_UPDATE_FAMILY_OPENPWD_QUEUE)
    public void process(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        //LOG.info("RabbitMQ_接收app用户id及项目id及开门密码，更新用户的家属开门密码:" + new String(body));
        try {
            TimeTaskDeadMessageQueuePO po= JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String,Object> map=JsonUtil.toObject(po.getMessageContent(),HashMap.class);
            //
//            iAppDeviceService.setUserQrCode(map.get("userId").toString(),map.get("projectId").toString());
            iAppMineService.updateUserRoomOpenPwd(map.get("userId").toString(),map.get("projectId").toString(),map.get("openPwd").toString());
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_接收app用户id及项目id及开门密码，更新用户的家属开门密码】:处理异常", e);
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

}
